<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>滨纷云S4图片上传下载测试</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        h1 {
            color: #333;
            text-align: center;
        }
        .section {
            margin-bottom: 30px;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        .file-input {
            margin-bottom: 10px;
        }
        .btn {
            padding: 8px 16px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        .btn:hover {
            background-color: #45a049;
        }
        .result {
            margin-top: 10px;
            padding: 10px;
            background-color: #f5f5f5;
            border-radius: 4px;
        }
        .file-list {
            list-style-type: none;
            padding: 0;
        }
        .file-list li {
            margin-bottom: 5px;
            padding: 8px;
            background-color: #f9f9f9;
            border-radius: 4px;
            display: flex;
            justify-content: space-between;
        }
        .file-actions {
            display: flex;
            gap: 10px;
        }
        .download-btn {
            background-color: #2196F3;
        }
        .download-btn:hover {
            background-color: #0b7dda;
        }
        .delete-btn {
            background-color: #f44336;
        }
        .delete-btn:hover {
            background-color: #da190b;
        }
    </style>
</head>
<body>
    <h1>滨纷云S4图片上传下载测试</h1>
    
    <div class="section">
        <h2>上传图片</h2>
        <div class="file-input">
            <input type="file" id="fileUpload" accept="image/*">
        </div>
        <button class="btn" onclick="uploadFile()">上传</button>
        <div class="result" id="uploadResult"></div>
    </div>
    
    <div class="section">
        <h2>图片列表</h2>
        <button class="btn" onclick="getFileList()">刷新列表</button>
        <ul class="file-list" id="fileList"></ul>
    </div>
    
    <script>
        // 上传文件
        function uploadFile() {
            const fileInput = document.getElementById('fileUpload');
            const resultDiv = document.getElementById('uploadResult');
            
            if (!fileInput.files[0]) {
                resultDiv.innerHTML = '请选择一个文件';
                return;
            }
            
            const formData = new FormData();
            formData.append('file', fileInput.files[0]);
            
            resultDiv.innerHTML = '上传中...';
            
            fetch('/api/s4/upload', {
                method: 'POST',
                body: formData
            })
            .then(response => response.json())
            .then(data => {
                if (data.error) {
                    resultDiv.innerHTML = `上传失败: ${data.error}`;
                } else {
                    // 直接显示文件名
                    resultDiv.innerHTML = `上传成功，文件名: ${data.fileName}`;
                    getFileList(); // 刷新文件列表
                }
            })
            .catch(error => {
                resultDiv.innerHTML = `上传出错: ${error}`;
            });
        }
        
        // 获取文件列表
        function getFileList() {
            const fileListElement = document.getElementById('fileList');
            fileListElement.innerHTML = '加载中...';
            
            fetch('/api/s4/list')
            .then(response => response.json())
            .then(files => {
                if (files.length === 0) {
                    fileListElement.innerHTML = '没有文件';
                    return;
                }
                
                fileListElement.innerHTML = '';
                files.forEach(file => {
                    // 直接使用返回的文件名
                    const li = document.createElement('li');
                    li.innerHTML = `
                        <span title="${file}">${file}</span>
                        <div class="file-actions">
                            <button class="btn download-btn" onclick="getPresignedUrl('${file}')">获取下载链接</button>
                            <button class="btn download-btn" onclick="downloadFile('${file}')">直接下载</button>
                            <button class="btn delete-btn" onclick="deleteFile('${file}')">删除</button>
                        </div>
                    `;
                    fileListElement.appendChild(li);
                });
            })
            .catch(error => {
                fileListElement.innerHTML = `加载出错: ${error}`;
            });
        }
        
        // 获取预签名URL
        function getPresignedUrl(key) {
            fetch(`/api/s4/presigned?key=${encodeURIComponent(key)}`)
            .then(response => response.json())
            .then(data => {
                if (data.url) {
                    // 创建一个临时的a标签并点击它
                    const a = document.createElement('a');
                    a.href = data.url;
                    a.target = '_blank';
                    a.click();
                }
            })
            .catch(error => {
                alert(`获取预签名URL出错: ${error}`);
            });
        }
        
        // 直接下载文件
        function downloadFile(key) {
            window.location.href = `/api/s4/download?key=${encodeURIComponent(key)}`;
        }
        
        // 删除文件
        function deleteFile(key) {
            if (!confirm(`确定要删除文件 ${key} 吗？`)) {
                return;
            }
            
            fetch(`/api/s4/delete?key=${encodeURIComponent(key)}`, {
                method: 'DELETE'
            })
            .then(response => response.json())
            .then(data => {
                if (data.error) {
                    alert(`删除失败: ${data.error}`);
                } else {
                    alert('删除成功');
                    getFileList(); // 刷新文件列表
                }
            })
            .catch(error => {
                alert(`删除出错: ${error}`);
            });
        }
        
        // 页面加载时获取文件列表
        window.onload = getFileList;
    </script>
</body>
</html> 